package org.therandoms.textgen;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * This is a class for representing a possible production for a production category. 
 * @author Logan Widick
 * @author Vinh Pham
 */
public class Production<T extends Serializable> implements Serializable{
	/**
	 * 
	 */
	private static final long serialVersionUID = -7450632920319373020L;

	/**
	 * Stores the symbols associated with the production
	 */
	private ArrayList<T> symbols = new ArrayList<T>();
	
	/**
	 * Stores the delimiter for printing
	 */
	private String delimiter = " ";
	
	/**
	 * Adds a symbol to the production
	 * @param sym the symbol to add
	 */
	public void addSymbolToProduction(T sym){
		if(sym == null){
			return;
		}
		symbols.add(sym);
	}
	
	/**
	 * Returns true if no symbols exist
	 * @return true if no symbols exist; otherwise false
	 */
	public boolean isEmpty(){
		if(symbols == null){
			return true;
		}
		else if(symbols.isEmpty()){
			return true;
		}
		else{
			return false;
		}
	}
	
	/**
	 * Removes a symbol from the production
	 * @param sym the symbol to remove
	 */
	public void removeSymbolFromProduction(T sym){
		if(sym == null){
			return;
		}
		symbols.remove(sym);
	}
	
	/**
	 * Gets the symbols associated with the production
	 * @return the symbols associated with the production
	 */
	public List<T> getSymbols(){
		return symbols;
	}
	
	/**
	 * Clears the production
	 */
	public void clearProduction(){
		symbols.clear();
	}
	
	/**
	 * Sets the new delimiter for printing
	 * @param newDelim the new delimiter for printing.
	 */
	public void setDelimiter(String newDelim){
		if(newDelim != null){
			delimiter = newDelim;
		}
	}
	
	/**
	 * Gets the delimiter for printing
	 * @return the delimiter for printing
	 */
	public String getDelimiter(){
		return delimiter;
	}
	
	/**
	 * Appends the symbols in this production to the end of a list. 
	 * @param outList The list to append to.
	 */
	public void appendSymbolsToList(List<T> outList){
		for(T symbol : symbols){
			outList.add(symbol);
		}
	}
	
	@Override
	public String toString(){
		StringBuilder sb = new StringBuilder();
		boolean hasGone = false;
		for(T symbol : symbols){
			if(hasGone){
				sb.append(delimiter);
			}
			sb.append(symbol);
			hasGone = true;
		}
		
		return sb.toString();
	}
}
